<?php
/**
 * 活动表
 * Date: 2021年3月4日
 * Time: 18:06:03
 */

namespace App\Model\BbcModel;

use Service\Code\Normal;
use App\Model\BaseModel\BaseBbcModel;

class ActivitySend extends BaseBbcModel {

    public $table = 'ly_activity_send';

    /**
     * 获取活动详情描述
     */
    public static function getContent($param) {
        if (!$param['activity_id']) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument activity_id');
        }
        $list = self::where('activity_id', $param['activity_id'])->get()->toArray();
        if (empty($list)) {
            return Normal::returnCode(Normal::FAIL, '活动详情不存在');
        }
        $content       = '';
        $sendGoods     = [];
        $goods_ids_arr = explode(',', implode(',', array_column($list, 'goods_ids')));
        $goodsAll      = Goods::whereIn('id', $goods_ids_arr)->select(['img', 'id'])->get();
        foreach ($list as $k => $v) {
            $unit = Activity::$unit[$v['unit_type']];
            if ($v['unit_type'] == 0) {
                $content .= '满' . round($v['full'], 2) . '赠' . round($v['send']) . '件，';
                $title   = '满' . round($v['full'], 2) . '赠' . round($v['send']) . '件';
            } else {
                $content .= '满' . round($v['full'], 2) . $unit . '赠' . round($v['send']) . '件，';
                $title   = '满' . round($v['full'], 2) . $unit . '赠' . round($v['send']) . '件';
            }
            $goodsIms       = $goodsAll->whereIn('id', explode(',', $v['goods_ids']))->pluck('img')->toArray();
            $goods['img']   = $goodsIms ?: [];
            $goods['title'] = $title;
            $sendGoods[]    = $goods;
        }
        $content = rtrim($content, '，');
        return Normal::returnCode(Normal::SUC, '获取成功', ['list' => $list, 'content' => $content, 'sendGoods' => $sendGoods]);
    }
}